package com.alisls.shequtao.service.goods.service.impl;

import com.alisls.shequtao.service.goods.dao.SkuDAO;
import com.alisls.shequtao.service.goods.dao.SkuDetailDAO;
import com.alisls.shequtao.service.goods.dto.SkuDTO;
import com.alisls.shequtao.service.goods.dto.SkuDetailDTO;
import com.alisls.shequtao.service.goods.entity.SkuDO;
import com.alisls.shequtao.service.goods.entity.SkuDetailDO;
import com.alisls.shequtao.service.goods.service.SkuService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商品特有属性服务
 *
 * @author Ke Wang
 */
@Service("skuService")
@AllArgsConstructor
@Slf4j
public class SkuServiceImpl extends ServiceImpl<SkuDAO, SkuDO> implements SkuService {

    private final SkuDAO skuDAO;

    private final SkuDetailDAO skuDetailDAO;

    @Override
    public List<SkuDTO> listSkusBySpuId(Long spuId) {
        QueryWrapper<SkuDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("spu_id", spuId);
        List<SkuDO> skuDOs = skuDAO.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(skuDOs)) {
            return null;
        }
        return convertToDTOs(skuDOs);
    }

    @Override
    public void saveSkuAndSkuDetail(List<SkuDetailDTO> skuDetailDTOs) {
        if (CollectionUtils.isEmpty(skuDetailDTOs)) {
            return;
        }

        skuDetailDTOs.forEach(skuDetailDTO -> {
            SkuDO skuDO = convertToDO(skuDetailDTO);
            if (skuDAO.insert(skuDO) == 1) {
                SkuDetailDO skuDetailDO = convertToDetailDO(skuDetailDTO);
                skuDetailDO.setId(skuDO.getId());
                skuDetailDAO.insert(skuDetailDO);
            }
        });
    }

    @Override
    public SkuDTO convertToDTO(SkuDO skuDO) {
        SkuDTO skuDTO = new SkuDTO();
        BeanUtils.copyProperties(skuDO, skuDTO);
        return skuDTO;
    }

    @Override
    public SkuDO convertToDO(SkuDTO skuDTO) {
        SkuDO skuDO = new SkuDO();
        BeanUtils.copyProperties(skuDTO, skuDO);
        String goodsPrice = skuDTO.getGoodsPrice();
        if (!StringUtils.isEmpty(goodsPrice)) {
            skuDO.setPrice(Long.parseLong(goodsPrice));
        }
        skuDO.setSpuId(skuDTO.getSpuDTO().getId());
        return skuDO;
    }

    public List<SkuDTO> convertToDTOs(List<SkuDO> skuDOs) {
        if (CollectionUtils.isEmpty(skuDOs)) {
            return null;
        }

        return skuDOs.stream().map(this::convertToDTO).collect(Collectors.toList());
    }

    public SkuDetailDO convertToDetailDO(SkuDTO skuDTO) {
        SkuDetailDO skuDetailDO = new SkuDetailDO();
        BeanUtils.copyProperties(skuDTO, skuDetailDO);
        return skuDetailDO;
    }

}
